package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.VisitRequestBody;
import com.example.elderlycare.dao.entity.CheckRecord;
import com.example.elderlycare.dao.entity.Visit;
import com.example.elderlycare.service.CheckRecordService;
import com.example.elderlycare.service.VisitService;
import com.example.elderlycare.service.entity.VisitListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/visit")
@Slf4j
public class VisitController {

    @Resource
    VisitService visitService;

    @Resource
    CheckRecordService checkRecordService;

    /**
     * 查询探视来访记录
     *
     * @param id        来访记录id
     * @param name      来访人姓名
     * @param nurseName 养老眼名字
     * @param startTime 根据时间范围查询的开始时间
     * @param endTime   根据时间范围查询的结束时间
     * @param pageSize  页大小
     * @param current   当前页
     * @return VisitListResponseBody
     */
    @GetMapping("/query")
    public Result<VisitListResponseBody> queryVisits(
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "nurseName", required = false) String nurseName,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        params.put("name", name != null ? "%" + name + "%" : null);
        params.put("nurseName", nurseName != null ? "%" + nurseName + "%" : null);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        try {
            VisitListResponseBody res = visitService.queryVisits(params);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 新增探视来访记录
     *
     * @param req VisitRequestBody.addVisit
     * @return null
     */
    @PostMapping("/add")
    public Result<?> addVisits(@RequestBody VisitRequestBody.addVisit req) {
        /* 获取入住登记记录 */
        Integer checkRecordId = req.getCheckRecordId();
        CheckRecord checkRecord = checkRecordService.getById(checkRecordId);

        Visit visit = Visit.builder()
                .elderId(checkRecord.getElderId())
                .name(req.getName())
                .certificateId(req.getCertificateId())
                .number(req.getNumber())
                .nurseId(checkRecord.getNurseId())
                .phone(req.getPhone())
                .startTime(new Date())
                .build();
        try {
            boolean flag = visitService.save(visit);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("add visit record error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }

    }

    /**
     * 修改探视来访记录
     *
     * @param req VisitRequestBody.updateVisit
     * @return null
     */
    @PutMapping("/update")
    public Result<?> updateVisits(@RequestBody VisitRequestBody.updateVisit req) {
        Visit visit = Visit.builder()
                .id(req.getId())
                .endTime(req.getEndTime())
                .name(req.getName())
                .certificateId(req.getCertificateId())
                .number(req.getNumber())
                .nurseId(req.getNurseId())
                .phone(req.getPhone())
                .elderId(req.getElderId())
                .build();
        try {
            boolean flag = visitService.updateById(visit);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update visit record error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 删除探视来访记录
     *
     * @param id 探视来访记录id
     * @return null
     */
    @DeleteMapping("/delete")
    public Result<?> deleteVisits(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = visitService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete visit error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
